package cn.micai.base.concurrent;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/7/9 15:08
 * @Description:
 */
public class Memoizer1<A, V> implements Computable<A, V> {

    private final Map<A, V> cache = new HashMap<>();
    private final Computable<A, V> c;

    public Memoizer1(Computable<A, V> c) {
        this.c = c;
    }

    @Override
    public synchronized V compute(A arg) throws InterruptedException {
        V v = cache.get(arg);
        if (v == null) {
            v = c.compute(arg);
            cache.put(arg, v);
        }
        return v;
    }
}
